package com.log.service.Impl;
import com.log.common.util.PageHelper;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.LogEmployeerMapper;
import com.log.pojo.LogCar;
import com.log.pojo.LogEmployeer;
import com.log.service.LogEmployeerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class LogEmployeerServiceImpl implements LogEmployeerService {
    @Autowired
    private LogEmployeerMapper logEmployeerMapper;

    //分页查询
    @Override
    public ResultVo selectLogEmployeerListByPage(int pageNum, int limit) {
        //1.起始索引
        int start = (pageNum - 1) * limit;
        //2.查询结果
        List<LogEmployeer> list = logEmployeerMapper.selectLogEmployeerListByPage(start, limit);
        //3.查询总数
        tk.mybatis.mapper.entity.Example example = new tk.mybatis.mapper.entity.Example(LogEmployeer.class);
        Example.Criteria criteria = example.createCriteria();
        int count = logEmployeerMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count % limit == 0 ? count / limit : count / limit + 1;
        return new ResultVo(ResStatus.OK, "success", new PageHelper<LogEmployeer>(count, pageCount, list));
    }

    //通过id查询
    @Override
    public ResultVo getLogEmployeerById(int id) {
        LogEmployeer logEmployeer = logEmployeerMapper.selectByPrimaryKey(id);
        return new ResultVo(ResStatus.OK, "sucess", logEmployeer);
    }

    //通过id删除
    @Override
    public ResultVo deleteLogEmployeerById(int id) {
        int logEmployeer = logEmployeerMapper.deleteByPrimaryKey(id);
        return new ResultVo(ResStatus.OK, "delete sucess", logEmployeer);
    }

    //添加员工信息
    @Override
    public ResultVo insertLogEmployeerById(LogEmployeer logEmployeer) {
        Example example = new Example(LogEmployeer.class);
        Example.Criteria criteria = example.createCriteria();
        //通过手机号码判断员工是否已经存在
        criteria.andEqualTo("logEmployeerPhone", logEmployeer.getLogEmployeerPhone());
        List<LogEmployeer> logEmployeers = logEmployeerMapper.selectByExample(example);
        if (logEmployeers.size() > 0) {
            //该员工已存在
            return new ResultVo(ResStatus.NO, "该员工号码已存在", null);
        } else {
            //将该员工添加进入数据库
            logEmployeerMapper.insert(logEmployeer);
            return new ResultVo(ResStatus.OK, "添加员工成功", null);
        }
    }

    //修改员工信息
    @Override
    public ResultVo updateLogEmployeer(LogEmployeer logEmployeer) {
        logEmployeerMapper.updateByPrimaryKeySelective(logEmployeer);
        return new ResultVo(ResStatus.OK, "修改成功", logEmployeer);
    }

    @Override
    public ResultVo getAllLogEmployeerInfoByPageAndStatus(int pageNum, int limit, int state) {
        //1.起始索引
        int start = (pageNum - 1) * limit;
        //2
        List<LogEmployeer> logEmployeers = logEmployeerMapper.selectLogEmployeerByPageAndStatus(start,limit,state);
        //3
        Example example = new Example(LogEmployeer.class);
       Example.Criteria criteria = example.createCriteria();
       criteria.andEqualTo("logEmployeerState",state);
     criteria.andEqualTo("logEmployeeJob","司机");
       int count = logEmployeerMapper.selectCountByExample(example);
       //4
        int pageCount = count%limit==0 ? count/limit:count/limit+1;

       return new ResultVo(ResStatus.OK,"sucess",new PageHelper<LogEmployeer>(count,pageCount,logEmployeers));
    }

}





